// 加载进度条工具集合
const chalk = require('chalk');
const CLI = require('clui')

class Loading {
  static start(message) {
    Loading.elapsed = 0
    Loading.message = message
    const Spinner = CLI.Spinner;
    Loading.instance = new Spinner(message, ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);  // ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']
    Loading.instance.start();
    setInterval(() => {
      Loading.elapsed++
      Loading.instance.message(`${chalk.blue(message)}${Loading.desc?`[${Loading.desc}]`:''}` + chalk.red(`(${Loading.elapsed}s)`));
    },1000)
  }

  static update(message) {
    if(typeof message !== 'string') return new Error('loading 信息必须是字符串')
    Loading.desc = message.substring(0,50) + message.length>50?'...':''
  }

  static end() {
    process.stdout.write('\n');
    Loading.instance.stop()
    return {
      elapsed: Loading.elapsed
    }
  }
}

Loading.instance = null
Loading.meessage = ''
Loading.desc = ''

module.exports = Loading